package butines.core.game.util;

import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import butines.core.Image;

public class ImageHelper {

	private static ImageHelper instance = new ImageHelper();
	
	public static ImageHelper getInstance() {
		return instance;
	}
	
	private final ColorPalleta DEFAULT_PALLETA = ColorPalleta.RED;

	public Image createImagePalleta(Image image, ColorPalleta palleta) {
		BufferedImage buffered = createBufferedImage(image.getRawImage());
		palleta.convert(buffered, DEFAULT_PALLETA);
		image = new Image(buffered);
		return image;
	}

	public Image createIsometricImage(Image image, int numAngle) {
		java.awt.Image rawImage = image.getRawImage();
		int width = rawImage.getWidth(null);
		int height = rawImage.getHeight(null);
		int transparency = getColorModel(rawImage).getTransparency();

		int numOrigem = (numAngle / 2) + 1;
		int widthFrame = (width / numOrigem);
		int newWidth = widthFrame * numAngle;

		int dx1 = width;
		int dx2 = width + (widthFrame * (numOrigem - 2)); 

		int sx1 = widthFrame + (widthFrame * (numOrigem - 2));
		int sx2 = widthFrame;
		
		BufferedImage buffered = createBufferedImage(newWidth, height, transparency);
		Graphics g = buffered.createGraphics();
		 // Desenha a imagem normal
		g.drawImage(rawImage, 0, 0, null);
		 // Desenha a imagem invertida descartando a primeira e ultima imagem
		g.drawImage(rawImage, dx1, 0, dx2, height, sx1, 0, sx2, height, null);
		g.dispose();

		return new Image(buffered);
	}
	
	public Image createImage(Image source, ColorPalleta palleta, int numAngle) {
		return createIsometricImage(createImagePalleta(source, palleta), numAngle);
	}
	
	public BufferedImage createBufferedImage(int width, int height, int transparency) {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, transparency);
	}

	public ColorModel getColorModel(java.awt.Image rawImage) {
		if (rawImage instanceof BufferedImage) {
			BufferedImage bimage = (BufferedImage) rawImage;
			return bimage.getColorModel();
		}
		
		PixelGrabber pg = new PixelGrabber(rawImage, 0, 0, 1, 1, false);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			// ignored
		}

		// Get the image's color model
		return pg.getColorModel();
	}

	public BufferedImage createBufferedImage(java.awt.Image rawImage) {
		int width = rawImage.getWidth(null);
		int height = rawImage.getHeight(null);
		ColorModel colorModel = getColorModel(rawImage);

		BufferedImage buffered = null;
		try {
			buffered = createBufferedImage(width, height, colorModel.getTransparency());
		} catch (HeadlessException e) {
			// The system does not have a screen
		}

		if (buffered == null) {
			int type = colorModel.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; 
			buffered = new BufferedImage(width, height, type);
		}

		Graphics g = buffered.createGraphics();
		g.drawImage(rawImage, 0, 0, null);
		g.dispose();

		return buffered;
	}

	public static void main(String[] args) throws IOException {
		ImageHelper helper = new ImageHelper();
		Image i = new Image("bin/footman.png");
		Image b = helper.createImage(i, ColorPalleta.BLUE, 8);
		ImageIO.write((BufferedImage)b.getRawImage(), "png", new File("./test.png"));
		System.out.println("feito");
	}

}
